package com.me.utils;


import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.text.TextUtils;

import java.util.UUID;


/**
 * CommSharedUtils
 * 通用的SharedPreferences
 */
public class Pref {

    private static final String SHARED_PATH = "info";
    private static Pref helper;
    private SharedPreferences sharedPreferences;


    private Pref(Context context) {
        sharedPreferences = context.getSharedPreferences(SHARED_PATH, Context.MODE_PRIVATE);
    }

    public static Pref getInstance(Context context) {
        if (helper == null) {
            helper = new Pref(context);
        }
        return helper;
    }

    public void putInt(String key, int value) {
        Editor edit = sharedPreferences.edit();
        edit.putInt(key, value);
        edit.apply();
    }

    public int getInt(String key) {
        return sharedPreferences.getInt(key, 0);
    }


    public void putString(String key, String value) {
        Editor edit = sharedPreferences.edit();
        edit.putString(key, value);
        edit.apply();
    }

    public String getString(String key, String v) {
        return sharedPreferences.getString(key, v);
    }

    public String getString(String key) {
        return sharedPreferences.getString(key, null);
    }

    public String getUUId() {
        String id = getString("uuid");
        if (TextUtils.isEmpty(id)) {
            id = UUID.randomUUID().toString();
            putString("uuid", id);
        }
        id = id.replaceAll("-", "");
        return id;
    }

    public void putBoolean(String key, boolean value) {
        Editor edit = sharedPreferences.edit();
        edit.putBoolean(key, value);
        edit.apply();
    }


    public boolean getBoolean(String key, boolean defValue) {
        return sharedPreferences.getBoolean(key, defValue);
    }

    public long getLong(String key, long defValue) {
        return sharedPreferences.getLong(key, defValue);
    }

    public void putLong(String key, long value) {
        Editor edit = sharedPreferences.edit();
        edit.putLong(key, value);
        edit.apply();
    }

    public void remove(String key) {
        Editor edit = sharedPreferences.edit();
        edit.remove(key);
        edit.apply();
    }

}
